package com.tl.bootmeter.service.impl;

import com.tl.bootmeter.mapper.WellModeMapper;
import com.tl.bootmeter.pojo.WellMode;
import com.tl.bootmeter.pojo.WellStation;
import com.tl.bootmeter.service.IWellModeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class WellModeServiceImpl implements IWellModeService {
    @Autowired
    private WellModeMapper wellModeMapper;
    /**
     * 根据站 id查询这个站下一共有多少井
     * 并且将页面需要的站id和站名称一同封装进map中返回给页面
     * @param station_id 页面传入的站id
     * @return stationIdWellModeMap 给控制器返回一个map
     */
    @Override
    public Map<String, Object> selectBystationIdWellMode(int station_id) {
        Map<String, Object> stationIdWellModeMap = new HashMap<String, Object>();
        //根据站id查询所有的井信息
        List<WellMode> wellmodeList = wellModeMapper.selectBystationIdWellMode(station_id);
        System.out.println("查询出来所有的井信息="+wellmodeList);
        //根据站id查询站信息
        String station_name = wellModeMapper.selectStationNameBystationId(station_id);
        System.out.println("查询出来的站信息="+station_name);
        //将所有信息封装到map中
        stationIdWellModeMap.put("station_id", station_id);
        stationIdWellModeMap.put("station_name", station_name);
        stationIdWellModeMap.put("wellmodeList", wellmodeList);
        return stationIdWellModeMap;
    }
    //新增油井
    @Override
    public int insertByWellMode(WellMode wellMode, int station_id) {
        //获取页面上的井号
        String namber = wellMode.getWell_namber();
        //获取计算机当前的时间戳
        Integer time = (int)(System.currentTimeMillis()/1000);
        //将时间戳转化成string类型
//		String time1 = String.valueOf(time);
        //将字符串拼接起来
//		String well_stringId = time1+namber;
        //再讲拼接起来的字段转成long类型
        long well_longId = Long.valueOf(time);
        wellMode.setWell_id(well_longId);
        int endwellmode = wellModeMapper.insertByWellMode(wellMode);
        //将需要的数据封装到wellstation类中
        WellStation ws = new WellStation();
        ws.setWell_id(well_longId);
        ws.setStation_id(station_id);
        //并且将井与站的中间表添加数据
        int wsend = wellModeMapper.insertWellStationByStationId(ws);
        if(endwellmode == 1 || wsend == 1){
            return 1;
        }else{
            return 0;
        }
    }
}
